
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>正则 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Proxy.html" />
    
    
    <link rel="prev" href="数字和Math.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="变量.html">
            
                <a href="变量.html">
            
                    
                    变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="解构.html">
            
                <a href="解构.html">
            
                    
                    解构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="数组.html">
            
                <a href="数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="对象.html">
            
                <a href="对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="prototype.html">
            
                <a href="prototype.html">
            
                    
                    prototype
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="Promise.html">
            
                <a href="Promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="模块化.html">
            
                <a href="模块化.html">
            
                    
                    模块化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="class和继承.html">
            
                <a href="class和继承.html">
            
                    
                    class和继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="this.html">
            
                <a href="this.html">
            
                    
                    this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="Symbol和generator.html">
            
                <a href="Symbol和generator.html">
            
                    
                    Symbol和generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="async_await.html">
            
                <a href="async_await.html">
            
                    
                    async_await
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="Set和WeakSet.html">
            
                <a href="Set和WeakSet.html">
            
                    
                    Set和WeakSet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Map和WeakMap.html">
            
                <a href="Map和WeakMap.html">
            
                    
                    Map和WeakMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="数字和Math.html">
            
                <a href="数字和Math.html">
            
                    
                    数字和Math
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.18" data-path="正则.html">
            
                <a href="正则.html">
            
                    
                    正则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="Proxy.html">
            
                <a href="Proxy.html">
            
                    
                    Proxy
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >正则</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6B63;&#x5219;">&#x6B63;&#x5219;</h1>
<p><a href="https://www.w3school.com.cn/jsref/jsref_obj_regexp.asp" target="_blank">https://www.w3school.com.cn/jsref/jsref_obj_regexp.asp</a>  </p>
<h3 id="&#x53CD;&#x5411;&#x5F15;&#x7528;">&#x53CD;&#x5411;&#x5F15;&#x7528;</h3>
<p>&#x4E00;&#x4E9B;&#x6982;&#x5FF5;:</p>
<ul>
<li><p>&#x5B50;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;&#x7528;()&#x5305;&#x56F4;&#x7684;&#x5C31;&#x662F;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;</p>
<pre><code>str = &quot;((\d)(\w))&quot;
&#x7B2C;&#x4E00;&#x4E2A;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;((\d)(\w))
&#x7B2C;&#x4E8C;&#x4E2A;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;(\d)
&#x7B2C;&#x4E09;&#x4E2A;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;(\w)
</code></pre></li>
<li><p>&#x6355;&#x83B7;&#xFF1A;&#x5C06;&#x5339;&#x914D;&#x5230;&#x7684;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;&#x4FDD;&#x5B58;&#x5728;RegExp&#x5BF9;&#x8C61;&#x4E2D;</p>
<pre><code>RegExp.$1&#xFF1A;&#x4FDD;&#x5B58;&#x7B2C;&#x4E00;&#x4E2A;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;
RegExp.$2&#xFF1A;&#x4FDD;&#x5B58;&#x7B2C;&#x4E8C;&#x4E2A;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;
RegExp.$3&#xFF1A;&#x4FDD;&#x5B58;&#x7B2C;&#x4E09;&#x4E2A;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;
...
</code></pre></li>
<li><p>&#x53CD;&#x5411;&#x5F15;&#x7528;&#xFF1A;&#x8C03;&#x7528;&#x6355;&#x83B7;&#x5230;&#x7684;&#x503C;(&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x7701;&#x53BB;&#x91CD;&#x590D;&#x7F16;&#x5199;&#x6B63;&#x5219;)</p>
<pre><code>\1&#xFF1A;&#x6307;&#x5411;RegExp.$1
\2&#xFF1A;&#x6307;&#x5411;RegExp.$2
\n&#xFF1A;&#x6307;&#x5411;RegExp.$n
</code></pre></li>
</ul>
<p>&#x6CE8;:   </p>
<ul>
<li>&#x5982;&#x679C;&#x8981;&#x5339;&#x914D;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;str&#x4E4B;&#x4E2D;&quot;&#x4E00;&#x6BB5;&#x4E00;&#x6BB5;&#x7684;&quot;&#x7684;&#x5C0F;&#x5B57;&#x7B26;&#x4E32;&#x7B26;&#x5408;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;, &#x9002;&#x5408;&#x4F7F;&#x7528; <code>str.match(regex)</code>&#x5E76;&#x7ED3;&#x5408;&#x5206;&#x7EC4;&#x6355;&#x83B7;&#x627E;&#x5230;&#x8FD9;&#x4E9B;&#x7247;&#x65AD;&#x5B57;&#x7B26;&#x4E32;, &#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5339;&#x914D;&#x7684;&#x6570;&#x7EC4;; &#x6BD4;&#x5982;&#x5728;&#x5B57;&#x7B26;&#x4E32; <code>ooo1212ooo2323ooo3434ooo1234</code> &#x4E2D;&#x5339;&#x914D;&#x548C;&#x6355;&#x83B7;&#x7C7B;&#x578B;&#x4E3A;xyxy&#x6837;&#x5F0F;&#x7684;&#x5B57;&#x7B26;&#x4E32;: <code>let reg = /(\d)(\d)\1\2/g;  let resultArray = str.match(reg)</code></li>
<li>&#x5982;&#x679C;&#x8981;&#x67E5;&#x8BE2;&#x4E00;&#x6574;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x7B26;&#x5408;&#x67D0;&#x4E2A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;, &#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>regex.test(str)</code>, &#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x503C;&#x4EE3;&#x8868;&#x6B63;&#x5219;&#x662F;&#x5426;&#x5339;&#x914D;&#x4E8E;&#x5B57;&#x7B26;&#x4E32;, &#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>RegExp.$1/2/3...</code>&#x83B7;&#x53D6;&#x5206;&#x7EC4;&#x4E2D;&#x7684;&#x6570;&#x636E; </li>
</ul>
<p>&#x793A;&#x4F8B;:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript">
        <span class="hljs-comment">//&#x5206;&#x7EC4;, &#x53CD;&#x5411;&#x5F15;&#x7528;&#x548C;&#x6355;&#x83B7;: &#x4ECE;&#x4E00;&#x4E2A;&#x5927;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x5339;&#x914D;&#x5230;&quot;&#x8FDE;&#x7EED;&#x76F8;&#x540C;&#x7684;&#x4E09;&#x4E2A;&#x6570;&#x5B57;&#x7684;&#x5C0F;&#x5B57;&#x7B26;&#x4E32;&quot;</span>
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test0</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">let</span> str = <span class="hljs-string">&apos;ooo111ooo222ooo333ooo123&apos;</span>;
            <span class="hljs-keyword">let</span> reg = <span class="hljs-regexp">/(\d)\1\1/g</span>; <span class="hljs-comment">//\d&#x4EE3;&#x8868;&#x6570;&#x636E;, &#x540E;&#x9762;&#x7684;\1&#x4EE3;&#x8868;&#x53CD;&#x5411;&#x5F15;&#x7528;&#x4E86;&#x524D;&#x9762;&#x7684;&#x5206;&#x7EC4;(\d),&#x5373;&#x548C;&#x524D;&#x9762;&#x7684;\d&#x503C;&#x76F8;&#x540C;</span>
            <span class="hljs-keyword">let</span> result = str.match(reg);
            <span class="hljs-built_in">console</span>.log(result);<span class="hljs-comment">//[&apos;111&apos;, &apos;222&apos;, &apos;333&apos;]</span>
        }

        <span class="hljs-comment">//&#x5206;&#x7EC4;, &#x53CD;&#x5411;&#x5F15;&#x7528;&#x548C;&#x6355;&#x83B7;: &#x5339;&#x914D;&#x5F62;&#x5F0F;&#x4E3A;xyxy&#x7684;&#x6570;&#x5B57;</span>
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test1</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">//str.match(regex)</span>
            <span class="hljs-keyword">let</span> str = <span class="hljs-string">&apos;ooo1212ooo2323ooo3434ooo1234&apos;</span>;
            <span class="hljs-keyword">let</span> reg = <span class="hljs-regexp">/(\d)(\d)\1\2/g</span>;<span class="hljs-comment">// \d&#x5339;&#x914D;&#x6570;&#x5B57;&#xFF0C;\1&#x5BF9;&#x5E94;&#x7684;&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;(\d)&#xFF0C;\2&#x5BF9;&#x5E94;&#x7684;&#x662F;&#x7B2C;&#x4E8C;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;(\d)&#xFF0C;g&#x662F;&#x5168;&#x5C40;&#x5339;&#x914D;</span>
            <span class="hljs-keyword">let</span> resultArr = str.match(reg); <span class="hljs-comment">//[&apos;1212&apos;, &apos;2323&apos;, &apos;3434&apos;]</span>
            <span class="hljs-built_in">console</span>.log(resultArr);
            <span class="hljs-built_in">console</span>.log(resultArr[<span class="hljs-number">0</span>]); <span class="hljs-comment">//1212</span>
            <span class="hljs-built_in">console</span>.log(resultArr[<span class="hljs-number">1</span>]); <span class="hljs-comment">//2323</span>
            <span class="hljs-built_in">console</span>.log(resultArr[<span class="hljs-number">2</span>]); <span class="hljs-comment">//3434</span>
        }

        <span class="hljs-comment">// regExp.test(str): &#x67E5;&#x770B;&#x67D0;&#x4E2A;&#x6B63;&#x5219;&#x662F;&#x5426;&#x5339;&#x914D;&#x5B57;&#x7B26;&#x4E32;</span>
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test2</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">//regex.test(str)</span>
            <span class="hljs-keyword">let</span> reg = <span class="hljs-regexp">/(\d{4})-(\d{2})-(\d{2})/</span>
            <span class="hljs-keyword">let</span> date = <span class="hljs-string">&apos;2010-04-12&apos;</span>
            <span class="hljs-keyword">if</span> (reg.test(date)) {
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x6B63;&#x5219;&#x5339;&#x914D;&quot;</span>);
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x6B63;&#x5219;&#x4E0D;&#x5339;&#x914D;&quot;</span>);
            }
        }

        <span class="hljs-comment">//&#x5339;&#x914D;&#x6807;&#x7B7E;&#x5185;&#x7684;&#x5185;&#x5BB9; //^([^f]|[^i]f)+$</span>
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test3</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-comment">// str.match(regex)</span>
            <span class="hljs-keyword">let</span> str = <span class="hljs-string">&apos;ooo&lt;div&gt;hello world!&lt;/div&gt;234&lt;div&gt;hello china!&lt;/div&gt;&apos;</span>;
            <span class="hljs-keyword">let</span> reg = <span class="hljs-regexp">/&lt;(\w+)&gt;[^&lt;]*&lt;\/\1&gt;/g</span>;
            <span class="hljs-keyword">let</span> result = str.match(reg);
            <span class="hljs-comment">/**
             * &#x5206;&#x6790;
             * \w&#xFF1A;  &#x67E5;&#x627E;&#x5B57;&#x6BCD;&#x3001;&#x6570;&#x5B57;&#x3001;&#x4E0B;&#x5212;&#x7EBF;
             * \w+&#xFF1A;  &#x5339;&#x914D;&#x67E5;&#x627E;&#x81F3;&#x5C11;&#x4E00;&#x4E2A;\w&#x7684;&#x5B57;&#x7B26;&#x4E32;
             * [^&lt;]&#xFF1A;  &#x67E5;&#x627E;&#x4E0D;&#x662F;&lt;&#x7684;&#x5B57;&#x7B26;
             * [^&lt;]*&#xFF1A;  &#x5339;&#x914D;&#x67E5;&#x627E;&#x81F3;&#x5C11;&#x96F6;&#x4E2A;[^&lt;]&#x7684;&#x5B57;&#x7B26;&#x4E32;
             * \/&#xFF1A;  &#x56E0;&#x4E3A;/&#x4E3A;&#x7279;&#x6B8A;&#x5B57;&#x7B26;&#xFF0C;&#x6240;&#x4EE5;&#x9700;&#x8981;&#x7528;&#x8F6C;&#x4E49;&#x7B26;\
             * \1&#xFF1A;  &#x53CD;&#x5411;&#x5F15;&#x7528;(\w+), &#x6BD4;&#x5982;&#x5206;&#x7EC4;&#x91CC;&#x7B2C;&#x4E00;&#x4E2A;div, &#x8FD9;&#x91CC;&#x4E5F;&#x662F;div
             */</span>
            <span class="hljs-built_in">console</span>.log(result[<span class="hljs-number">0</span>]); <span class="hljs-comment">//&lt;div&gt;hello world!&lt;/div&gt;</span>
            <span class="hljs-built_in">console</span>.log(result[<span class="hljs-number">1</span>]); <span class="hljs-comment">//&lt;div&gt;hello china!&lt;/div&gt;</span>
        }

        <span class="hljs-built_in">window</span>.onload=<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-comment">//test1();</span>
            <span class="hljs-comment">//test2();</span>
            test3();
        };
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>&#x793A;&#x4F8B;3:
<a href="https://www.jb51.net/article/79309.htm" target="_blank">https://www.jb51.net/article/79309.htm</a></p>
<pre><code class="lang-html">let reg = /(\d{4})-(\d{2})-(\d{2})/
let date = &apos;2010-04-12&apos;
reg.test(date)

RegExp.$1 // 2010
RegExp.$2 // 04
RegExp.$3 // 12
</code></pre>
<h3 id="regexp12">RegExp.$1/2/...</h3>
<p><a href="https://blog.csdn.net/qq_42423964/article/details/102385983" target="_blank">https://blog.csdn.net/qq_42423964/article/details/102385983</a></p>
<p><code>RegExp</code> &#x662F;javascript&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x5185;&#x7F6E;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x5BF9;&#x8C61;
RegExp.$1&#x662F;RegExp&#x7684;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;,&#x6307;&#x7684;&#x662F;&#x4E0E;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x5339;&#x914D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A; &#x5B50;&#x5339;&#x914D;(&#x4EE5;&#x62EC;&#x53F7;&#x4E3A;&#x6807;&#x5FD7;)&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4EE5;&#x6B64;&#x7C7B;&#x63A8;&#xFF0C;RegExp.$2&#xFF0C;RegExp.$3&#xFF0C;..RegExp.$99&#x603B;&#x5171;&#x53EF;&#x4EE5;&#x6709;99&#x4E2A;&#x5339;&#x914D;<br>RegExp&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x4F1A;&#x5728;&#x6211;&#x4EEC;&#x8C03;&#x7528;&#x4E86;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x65B9;&#x6CD5;&#x540E;, &#x81EA;&#x52A8;&#x5C06;&#x6700;&#x8FD1;&#x4E00;&#x6B21;&#x7684;&#x7ED3;&#x679C;&#x4FDD;&#x5B58;&#x5728;&#x91CC;&#x9762;, &#x6240;&#x4EE5;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5728;&#x4F7F;&#x7528;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x65F6;, &#x6709;&#x7528;&#x5230;&#x5206;&#x7EC4;, &#x90A3;&#x4E48;&#x5C31;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728;&#x8C03;&#x7528;&#x5B8C;&#x4EE5;&#x540E;&#x76F4;&#x63A5;&#x4F7F;&#x7528;RegExp.$xx&#x6765;&#x4F7F;&#x7528;&#x6355;&#x83B7;&#x5230;&#x7684;&#x5206;&#x7EC4;&#x5185;&#x5BB9;</p>
<p>&#x793A;&#x4F8B;:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
    <span class="hljs-keyword">let</span> reg = <span class="hljs-regexp">/(\d{4})-(\d{2})-(\d{2})/</span>
    <span class="hljs-keyword">let</span> date = <span class="hljs-string">&apos;2010-04-12&apos;</span>
    <span class="hljs-keyword">if</span> (reg.test(date)) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x6B63;&#x5219;&#x5339;&#x914D;&quot;</span>);
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x6B63;&#x5219;&#x4E0D;&#x5339;&#x914D;&quot;</span>);
    }
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">RegExp</span>.$<span class="hljs-number">1</span>); <span class="hljs-comment">// 2010</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">RegExp</span>.$<span class="hljs-number">2</span>); <span class="hljs-comment">// 04</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">RegExp</span>.$<span class="hljs-number">3</span>); <span class="hljs-comment">// 12</span>
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>&#x518D;&#x6765;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
    <span class="hljs-keyword">const</span> r = <span class="hljs-regexp">/^(\d{4})-(\d{1,2})-(\d{1,2})$/</span>
    r.exec(<span class="hljs-string">&apos;2019-10-08&apos;</span>)

    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">RegExp</span>.$<span class="hljs-number">1</span>)  <span class="hljs-comment">// 2019</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">RegExp</span>.$<span class="hljs-number">2</span>)  <span class="hljs-comment">// 10</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">RegExp</span>.$<span class="hljs-number">3</span>)  <span class="hljs-comment">// 08</span>
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>again:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
    <span class="hljs-keyword">let</span> str = <span class="hljs-string">&apos;ooo1212ooo2323ooo3434ooo1234&apos;</span>;
    <span class="hljs-keyword">let</span> reg = <span class="hljs-regexp">/(\d)(\d)\1\2/g</span>;<span class="hljs-comment">// \d&#x5339;&#x914D;&#x6570;&#x5B57;&#xFF0C;\1&#x5BF9;&#x5E94;&#x7684;&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;(\d)&#xFF0C;\2&#x5BF9;&#x5E94;&#x7684;&#x662F;&#x7B2C;&#x4E8C;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;(\d)&#xFF0C;g&#x662F;&#x5168;&#x5C40;&#x5339;&#x914D;</span>
    <span class="hljs-keyword">let</span> resultArr = str.match(reg); <span class="hljs-comment">//[&apos;1212&apos;, &apos;2323&apos;, &apos;3434&apos;]</span>
    <span class="hljs-built_in">console</span>.log(resultArr);
    <span class="hljs-built_in">console</span>.log(resultArr[<span class="hljs-number">0</span>]); <span class="hljs-comment">//1212</span>
    <span class="hljs-built_in">console</span>.log(resultArr[<span class="hljs-number">1</span>]); <span class="hljs-comment">//2323</span>
    <span class="hljs-built_in">console</span>.log(resultArr[<span class="hljs-number">2</span>]); <span class="hljs-comment">//3434</span>

    <span class="hljs-comment">// &#x6CE8;: &#x8FD9;&#x91CC;&#x4E3A;&#x4EC0;&#x4E48;&#x662F;3,4&#x5462;? &#x56E0;&#x4E3A; match &#x65B9;&#x6CD5;&#x662F;&#x4E00;&#x6BB5;&#x6BB5;&#x5339;&#x914D;&#x7684;,&#x6700;&#x540E;&#x4E00;&#x6B21;&#x5339;&#x914D;&#x7684;&#x662F; &apos;3434&apos;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">RegExp</span>.$<span class="hljs-number">1</span>)  <span class="hljs-comment">// 3</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">RegExp</span>.$<span class="hljs-number">2</span>)  <span class="hljs-comment">// 4</span>
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<h3 id="&#x547D;&#x540D;&#x6355;&#x83B7;">&#x547D;&#x540D;&#x6355;&#x83B7;</h3>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
        <span class="hljs-comment">// &#x9700;&#x6C42;:&#x53D6;&#x51FA;&#x5E74;&#x6708;&#x65E5;</span>
        <span class="hljs-keyword">let</span> str = <span class="hljs-string">&quot;2018-03-20&quot;</span>;
        <span class="hljs-comment">//&#x4F7F;&#x7528;&#x5206;&#x7EC4;</span>
        <span class="hljs-keyword">let</span> reg = <span class="hljs-regexp">/(\d{4})-(\d{2})-(\d{2})/</span>;
        <span class="hljs-keyword">let</span> dataArr = str.match(reg);
        <span class="hljs-built_in">console</span>.log(dataArr); <span class="hljs-comment">//[&apos;2018-03-20&apos;, &apos;2018&apos;, &apos;03&apos;, &apos;20&apos;, index: 0, input: &apos;2018-03-20&apos;, groups: undefined]</span>
        <span class="hljs-built_in">console</span>.log(dataArr[<span class="hljs-number">0</span>]); <span class="hljs-comment">//2018-03-20</span>
        <span class="hljs-built_in">console</span>.log(dataArr[<span class="hljs-number">1</span>]); <span class="hljs-comment">//2018</span>
        <span class="hljs-built_in">console</span>.log(dataArr[<span class="hljs-number">2</span>]); <span class="hljs-comment">//03</span>
        <span class="hljs-built_in">console</span>.log(dataArr[<span class="hljs-number">3</span>]); <span class="hljs-comment">//20</span>

        <span class="hljs-comment">//ES9(&#x5373;ES2018)&#x65B0;&#x589E;&#x4E86;&#x547D;&#x540D;&#x6355;&#x83B7;</span>
        <span class="hljs-keyword">let</span> reg2 = <span class="hljs-regexp">/(?&lt;year&gt;\d{4})-(?&lt;month&gt;\d{2})-(?&lt;day&gt;\d{2})/</span>;

        <span class="hljs-built_in">console</span>.log(str.match(reg2).groups); <span class="hljs-comment">//{year: &apos;2018&apos;, month: &apos;03&apos;, day: &apos;20&apos;}</span>
        <span class="hljs-keyword">let</span> {year, month, day} = str.match(reg2).groups;
        <span class="hljs-built_in">console</span>.log(year, month, day); <span class="hljs-comment">//2018 0 20</span>
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="&#x53CD;&#x5411;&#x5F15;&#x7528;&#x547D;&#x540D;&#x6355;&#x83B7;">&#x53CD;&#x5411;&#x5F15;&#x7528;&#x547D;&#x540D;&#x6355;&#x83B7;</h3>
<ul>
<li>&#x53CD;&#x5411;&#x5F15;&#x7528;: <code>\1 \2 $1 $2 (\&#x7528;&#x4E8E;&#x5339;&#x914D;, $&#x7528;&#x4E8E;&#x66FF;&#x6362;) ...</code></li>
<li>&#x53CD;&#x5411;&#x5F15;&#x7528;&#x547D;&#x540D;&#x6355;&#x83B7;: <code>\k&lt;&#x540D;&#x79F0;&gt;</code></li>
</ul>
<p>&#x793A;&#x4F8B;:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//&#x53CD;&#x5411;&#x5F15;&#x7528;&#x547D;&#x540D;&#x6355;&#x83B7;: `\k&lt;&#x540D;&#x79F0;&gt;`</span>
<span class="hljs-keyword">let</span> reg = <span class="hljs-regexp">/^(?&lt;daliu&gt;welcome)-\k&lt;daliu&gt;$/</span>;
<span class="hljs-comment">// &#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x540C;: /^(?&lt;daliu&gt;welcome)-\1$/</span>

<span class="hljs-keyword">let</span> str = <span class="hljs-string">&quot;a-a&quot;</span>;
<span class="hljs-keyword">let</span> str2 = <span class="hljs-string">&quot;daliu-daliu&quot;</span>;
<span class="hljs-keyword">let</span> str3 = <span class="hljs-string">&quot;welcome-welcome&quot;</span>;
<span class="hljs-built_in">console</span>.log(reg.test(str)); <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(reg.test(str2)); <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(reg.test(str3)); <span class="hljs-comment">//true</span>

<span class="hljs-keyword">let</span> {daliu} = str3.match(reg).groups;
<span class="hljs-built_in">console</span>.log(daliu); <span class="hljs-comment">//welcome</span>
<span class="hljs-comment">// console.log(str3.match(reg).groups); //daliu: &quot;welcome&quot;</span>
</code></pre>
<h3 id="&#x547D;&#x540D;&#x6355;&#x83B7;&#x548C;&#x66FF;&#x6362;">&#x547D;&#x540D;&#x6355;&#x83B7;&#x548C;&#x66FF;&#x6362;</h3>
<pre><code class="lang-javascript">&lt;script&gt;
    let str = &apos;2018-03-20&apos;;
    let reg = /(?&lt;year&gt;\d{4})-(?&lt;month&gt;\d{2})-(?&lt;day&gt;\d{2})/;
    //&#x53D8;&#x6210;: 03/20/2018
    let result = str.replace(reg, &apos;$&lt;month&gt;/$&lt;day&gt;/$&lt;year&gt;&apos;);
    console.log(result); //03/20/2018
&lt;/script&gt;
</code></pre>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x57FA;&#x4E8E;callback&#x7684;replace&#x65B9;&#x6CD5;:</p>
<pre><code class="lang-javascript">&lt;script&gt;
    <span class="hljs-keyword">let</span> str = <span class="hljs-string">&apos;2018-03-20&apos;</span>;
    <span class="hljs-keyword">let</span> reg = <span class="hljs-regexp">/(?&lt;year&gt;\d{4})-(?&lt;month&gt;\d{2})-(?&lt;day&gt;\d{2})/</span>;
    <span class="hljs-comment">//&#x53D8;&#x6210;: 03/20/2018</span>
    <span class="hljs-keyword">let</span> result = str.replace(reg, (...args) =&gt; {
        <span class="hljs-built_in">console</span>.log(args[args.length<span class="hljs-number">-1</span>]);
        <span class="hljs-comment">// args&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x662F;&#x4E00;&#x4E2A;json: {year: &apos;2018&apos;, month: &apos;03&apos;, day: &apos;20&apos;}</span>
        <span class="hljs-keyword">let</span> {year, month, day} = args[args.length<span class="hljs-number">-1</span>];
        <span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${day}</span>/<span class="hljs-subst">${month}</span>/<span class="hljs-subst">${year}</span>`</span>; <span class="hljs-comment">//20/03/2018</span>
    });
    <span class="hljs-built_in">console</span>.log(result);
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<h3 id="&#x7684;dotall&#x6A21;&#x5F0F;-s">.&#x7684;dotAll&#x6A21;&#x5F0F;: s</h3>
<p>&#x4E4B;&#x524D;&#x6B63;&#x5219;&#x4E2D;&#x7684;<code>.</code>&#x5339;&#x914D;&#x4EFB;&#x610F;&#x4E1C;&#x897F;,&#x4F46;&#x662F;&#x4E0D;&#x5305;&#x62EC; <code>\n</code>, ES2018&#x52A0;&#x5165;&#x7684;&#x6A21;&#x5F0F;&#x7684;&#x6982;&#x5FF5;</p>
<pre><code class="lang-javascript">&lt;script&gt;
    let reg = /^\w+.\w+$/;
    let str1 = &apos;welcome&#x221E;tom&apos;;
    console.log(reg.test(str1)); //true

    let str2 = &apos;welcome\ntom&apos;;
    console.log(reg.test(str2)); //false

    //&#x5F15;&#x5165;dotAll&#x6A21;&#x5F0F;: s
    let reg2 = /^\w+.\w+$/s; //&#x6DFB;&#x52A0;&#x4E86;s, &#x540E;&#x7F00;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;: gims
    console.log(reg2.test(str2)); //true
&lt;/script&gt;
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="数字和Math.html" class="navigation navigation-prev " aria-label="Previous page: 数字和Math">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Proxy.html" class="navigation navigation-next " aria-label="Next page: Proxy">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"正则","level":"1.18","depth":1,"next":{"title":"Proxy","level":"1.19","depth":1,"path":"Proxy.md","ref":"Proxy.md","articles":[]},"previous":{"title":"数字和Math","level":"1.17","depth":1,"path":"数字和Math.md","ref":"数字和Math.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"正则.md","mtime":"2022-01-03T11:25:20.821Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-02-17T15:54:39.156Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

